package cn.sinjet.model.carsystem;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import cn.sinjet.carassist.SinjetApplication;
import cn.sinjet.model.carassist.AppModel;
import cn.sinjet.model.dynamicfont.DynamicFontModel;
import cn.sinjet.model.mcuupgrade.McuUpgradeModel;
import cn.sinjet.model.mcuupgrade.McuUpgradeModelSimple;
import cn.sinjet.phone.PhoneModel;


public class ModelMsgHandler extends Handler{
//	MyApplication app;
	public final static int MSG_FROM_APP = 1;
	public final static int MSG_VIEW_EVENT = 2;
//	public final static int MSG_NAVI_INFO = 3;
	public final static int MSG_FROM_DEVICE = 5;
//	public final static int MSG_WAITING_TIMEOUT = 6;
//	public final static int MSG_UPDATE_UIOBJ = 7;
	public final static String KEY_MSG_DEVICE = "KEY_MSG_DEVICE";
//	public final static String KEY_NAME_WAITING = "KEY_WAITING_TIMEOUT";
	public final static String KEY_VIEW_EVENT = "KEY_VIEW_EVENT";
//	public final static String KEY_DEVICE_EVENT = "KEY_DEVICE_EVENT";
	public final static String KEY_NAVI_INFO = "KEY_NAVI_INFO";
	public final static String KEY_MSG_APP = "KEY_MSG_APP";
	//MyApplication app;
	static String tag = "model";
	CarModel models;
     public ModelMsgHandler(CarModel models, Looper looper) {
		// TODO Auto-generated constructor stub
    	 super(looper);
//    	 app = MyApplication.getInstance();
    	this.models =  models;
	}
     
     @Override
    public void handleMessage(android.os.Message msg) {
    	// TODO Auto-generated method stub
    	//super.handleMessage(msg);
    	 Log.d(tag,"msg:"+msg.what);
    	int what  = msg.what;
 		switch(what){
 		case MSG_VIEW_EVENT:{
 			int tag = msg.arg1;
// 			ViewEvent event = (ViewEvent)msg.obj;
	 		//switch(tag){
	 	//		default:
 			AppModel.getInstance().onClkView(tag);
		 	//		break;
	 		//	}
 			}
 			break;
 
 		case MSG_FROM_APP:{
 			 Bundle bundle = msg.getData();
		     byte[] buf = bundle.getByteArray(KEY_MSG_APP);
 			 handleAppMsg(buf);
 		   }
 			break;

 		case MSG_FROM_DEVICE:{
 			//UserModel.getIns().setHeatbeatRsp();
 			  Bundle bundle = msg.getData();
 		      byte[] buf = bundle.getByteArray(KEY_MSG_DEVICE);
// 		      models.devModel.onRecvMsg(buf);
 		      handMsgFromDevice(buf, buf.length);
 		    } 
 			break;
// 		case MSG_NAVI_INFO:{
// 			Bundle bundle = msg.getData();
//		    byte[] buf = bundle.getByteArray(KEY_NAVI_INFO);
//		    handleNaviInfo(buf);
// 		}break;
 		}
    }
     	
    void handMsgFromDevice(byte[] buf,int len){
    	if(len == 0|| buf == null) return;
//    	Log.i("BT","recvMessage len: "+ len);
//    	final StringBuilder stringBuilder = new StringBuilder(buf.length);
//		for (byte byteChar : buf)
//			stringBuilder.append(String.format("%02X ", byteChar));
//		Log.i("BT","recvMessage: "+ stringBuilder.toString());
    	switch(buf[0]){//mode ID
    	case 0x01:
    	case 0x03:
    		models.devModule.onRecvMsg(buf);
    		break;
    	case 0x04:
    		models.tpmsModule.onRecvTPMS(buf);
    		break;
    	case 0x05:
    	case 0x06:
    		models.controlModule.onRecvMsg(buf, buf.length);
    		break;
    	case 0x35:
    		PhoneModel.getIns(SinjetApplication.getInstance()).onRecvMsg(buf,buf.length);
    		break;
    	case 0x36:
			DynamicFontModel.getInstance().onRecvDynamicFontMsg(buf);
    		break;
    	case 0x37:
    	case 0x38:
    	case 0x39:
    		CarModel.getInstance().devModule.setMCUUpgradeMethod(1);
			McuUpgradeModel.getInstance().onRecvUpgradeMsg(buf);
    		break;
    	case 0x3A:
    		CarModel.getInstance().devModule.setMCUUpgradeMethod(2);
    		McuUpgradeModelSimple.getInstance().onRecvUpgradeMsg(buf);
    		break;
    	case 0x3F:{
    		int cmd = buf[1];
    		if(cmd == 0x10){
    			char[] charBuf = new char[len-2];
    			for(int i=0;i<charBuf.length;i++)
    				charBuf[i] = (char)buf[2+i];
    			String string = String.valueOf(charBuf);
    			AppModel.getInstance().setAgencyCode(string);
    		}
    	   }
    		break;
    	}
    }
     
   private void handleAppMsg(byte[] buf) {
		// TODO Auto-generated method stub
		byte cmd = buf[0];
		if(cmd == 0x1){
			byte pageId = buf[1];
			AppModel.getInstance().onEnterPage(pageId);
		}
			
	}
     
}
